package cn.usian.service.impl;

import cn.usian.common.R;
import cn.usian.dao.EmployeeDao;
import cn.usian.entity.Employee;
import cn.usian.exception.NameException;
import cn.usian.service.EmployeeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@Service
@Transactional
public class EmployeeServiceImpl implements EmployeeService {
    @Resource
    private EmployeeDao employeeDao;


    @Override
    public R<Employee> login(Employee employee) {
        //1.
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        //添加用户名的查询条件
        queryWrapper.eq(Employee::getUsername,employee.getUsername());
        //执行查询
        Employee selectOne = employeeDao.selectOne(queryWrapper);
        //如果用户名不存在直接返回错误信息
        if (selectOne==null){
            return  R.error("用户不存在");
        }
        //2.判定密码是否存在md5
        //获取用户输入的密码进行md5的转
        String hex= DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
        if (!hex.equals(selectOne.getPassword())){
            return R.error("密码不正确");
        }
        //3.判断状态
        if (selectOne.getStatus()==0){
            return R.error("账号已被警用");
        }
        return R.success(selectOne);
    }

    @Override
    public R<Page<Employee>> page(Integer page, Integer pageSize, String name) {

        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.like(name!=null,Employee::getName,name);

        Page<Employee> employeePage = employeeDao.selectPage(new Page<>(page, pageSize), queryWrapper);

        return R.success(employeePage);
    }


    @Override
    public void save(Employee employee) {
        //判断当前用户名是否存在
        //1.
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        //添加用户名的查询条件
        queryWrapper.eq(Employee::getUsername,employee.getUsername());
        //执行查询
        Employee selectOne = employeeDao.selectOne(queryWrapper);
       if (selectOne!=null){
            throw  new NameException(employee.getUsername());
       }
        //如果用户名不存在直接返回错误信息
        //保存用户获得创建时间
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        //设置默认密码
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()).toString());
        employeeDao.insert(employee);
    }

    @Override
    public R findById(Long id) {
        return R.success(employeeDao.selectById(id));
    }

    @Override
    public void updateEmp(Employee employee) {
        employee.setUpdateTime(LocalDateTime.now());
        LambdaUpdateWrapper<Employee> updateWrapper = new LambdaUpdateWrapper<>();
        //判断用户名 name
        if(employee.getName()!=null && !employee.getName().equals("")){
            updateWrapper.set(Employee::getName,employee.getName());
        }
        //判断账号 username
        if(employee.getUsername()!=null && !employee.getUsername().equals("")){
            updateWrapper.set(Employee::getUsername,employee.getUsername());
        }
        //判断手机号 phone
        if(employee.getPhone()!=null && !employee.getPhone().equals("")){
            updateWrapper.set(Employee::getPhone,employee.getPhone());
        }
        //判断性别 sex
        if(employee.getSex()!=null && !employee.getSex().equals("")){
            updateWrapper.set(Employee::getSex,employee.getSex());
        }
        //判断身份证 idNumber
        if(employee.getIdNumber()!=null && !employee.getIdNumber().equals("")){
            updateWrapper.set(Employee::getIdNumber,employee.getIdNumber());
        }
        //判断状态 status
        if(employee.getStatus()!=null){
            updateWrapper.set(Employee::getStatus,employee.getStatus());
        }
        updateWrapper.eq(Employee::getId,employee.getId());
        employeeDao.update(employee,updateWrapper);
    }


}
